import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/material.dart';

class BaseInteractive extends StatelessWidget {
  ///子组件
  final Widget? childWidget;

  ///点击事件
  final VoidCallback? onClick;

  ///双击事件
  final VoidCallback? onDoubleClick;

  ///长按事件
  final VoidCallback? onLongPress;

  ///是否监听滑动
  final bool isOnScroll;

  ///向左滑动
  final VoidCallback? onSlideLeft;

  ///向右滑动
  final VoidCallback? onSlideRight;

  ///基础交互组件
  BaseInteractive(
      {super.key,
      this.childWidget,
      this.onClick,
      this.onDoubleClick,
      this.onLongPress,
      this.onSlideLeft,
      this.onSlideRight,
      this.isOnScroll = false});

  @override
  Widget build(BuildContext context) {
    return isOnScroll
        ? GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: onClick,
            onDoubleTap: onDoubleClick,
            onLongPress: onLongPress,
            onHorizontalDragStart: _onHorizontalDragStart,
            onHorizontalDragUpdate: _onHorizontalDragUpdate,
            onHorizontalDragEnd: _onHorizontalDragEnd,
            child: childWidget ?? getWidget(context),
          )
        : GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: onClick,
            onDoubleTap: onDoubleClick,
            onLongPress: onLongPress,
            child: childWidget ?? getWidget(context),
          );
  }

  ///获取控件
  Widget? getWidget(BuildContext context) {
    return null;
  }

  var startPos;
  var endPos;

  ///横向滑动开始
  void _onHorizontalDragStart(DragStartDetails info) {
    // logger.d('开始 ${info.globalPosition}');
    startPos = info.globalPosition;
  }

  ///滑动过程
  void _onHorizontalDragUpdate(DragUpdateDetails info) {
    // logger.d('中间 ${info.globalPosition}');
    endPos = info.globalPosition;
  }

  ///滑动结束
  void _onHorizontalDragEnd(DragEndDetails info) {
    double direction = endPos.dx - startPos.dx;
    if (direction.isNegative) {
      //向左滑动
      onSlideLeft!();
    } else {
      //向右滑动
      onSlideRight!();
    }
  }
}
